/*
 * Copyright (c) 2006-2010, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.livejournal;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.panteleyev.ljapi.LJAccount;
import org.panteleyev.ljapi.LJEventAccessLevel;
import org.panteleyev.swing.BasicDialog;
import org.panteleyev.swing.OkCancelButtonPanel;
import org.panteleyev.swing.SimpleComboBoxModel;
import org.panteleyev.swing.SwingFactory;

/**
 *
 * @author  Petr Panteleyev
 */
final class ChangeAttributesDialog extends BasicDialog<JFrame> {
    private static String[] securityTypes = new String[] {
        L10N.LJ_SECURITY_PUBLIC.s(),
        L10N.LJ_SECURITY_PRIVATE.s(),
        L10N.LJ_SECURITY_FRIENDS.s(),
        L10N.LJ_SECURITY_CUSTOM.s()
    };
    
    private static String[] screenTypes = new String[] {
        L10N.LJ_SCREEN_DEFAULT.s(),
        L10N.LJ_SCREEN_NO.s(),
        L10N.LJ_SCREEN_ANONYMOUS.s(),
        L10N.LJ_SCREEN_NON_FRIENDS.s(),
        L10N.LJ_SCREEN_ALL.s(),
    };

    private LJAccount           m_account;
    private boolean             m_internalAccessChange;
    private LJEventAccessLevel  m_accessLevel;
    private int                 m_groupMask;
    private JCheckBox           userpicCheckbox;
    private JComboBox           userpicComboBox;
    private JTextField          locationTextEdit = new JTextField(25);
    private JCheckBox           locationCheckbox;
    private JCheckBox           musicCheckbox;
    private JTextField          musicTextEdit = new JTextField(25);
    private JCheckBox           moodCheckbox;
    private JComboBox           moodComboBox;
    private JComboBox           screenComboBox;
    private JCheckBox           screenCheckbox;
    private JCheckBox           accessLevelCheckbox;
    private JComboBox           accessLevelComboBox;
    private JButton             accessLevelButton;

    /** Creates new form ChangeAttributesDialog */
    public ChangeAttributesDialog(JFrame parent, LJAccount account) {
        super(parent);
        m_account = account;
        m_accessLevel = LJEventAccessLevel.PUBLIC;
        m_groupMask = 0;

        initComponents();

        pack();
    }

    private void initComponents() {
        setTitle(L10N.CHATTR_DIALOG_TITLE.s());
        getContentPane().setLayout(new BorderLayout());

        SimpleComboBoxModel moodDropDownModel = new SimpleComboBoxModel() {
            @Override
            public int getSize() { return m_account.getMoods().size(); }
            @Override
            public Object getElementAt(int index) { return m_account.getMoods().get(index).getName(); }
        };

        SimpleComboBoxModel userpicDropDownModel = new SimpleComboBoxModel(new Vector(m_account.getUserpicKeys()));

        JPanel panel = SwingFactory.createPanel(BorderFactory.createEmptyBorder(5, 5, 5, 5), SwingFactory.LAYOUT.BOX_Y,
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                accessLevelCheckbox = new JCheckBox(new AbstractAction(L10N.CHATTR_DIALOG_ACCESS_LEVEL_CHECKBOX.s()) {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        boolean b = accessLevelCheckbox.isSelected();
                        accessLevelComboBox.setEnabled(b);
                        accessLevelButton.setEnabled(b);
                    }
                }),
                Box.createHorizontalStrut(3),
                accessLevelComboBox = new JComboBox(securityTypes),
                Box.createHorizontalStrut(3),
                accessLevelButton = new JButton(new AbstractAction("...") {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        onEditAccessLevel();
                    }
                }),
                Box.createHorizontalGlue()
            ),
            Box.createVerticalStrut(5),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                screenCheckbox = new JCheckBox(new AbstractAction(L10N.CHATTR_DIALOG_SCREEN_COMMENTS_CHECKBOX.s()) {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        screenComboBox.setEnabled(screenCheckbox.isSelected());
                    }
                }),
                Box.createHorizontalStrut(3),
                screenComboBox = new JComboBox(screenTypes),
                Box.createHorizontalGlue()
            ),
            Box.createVerticalStrut(5),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                moodCheckbox = new JCheckBox(new AbstractAction(L10N.CHATTR_DIALOG_MOOD_CHECKBOX.s()) {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        moodComboBox.setEnabled(moodCheckbox.isSelected());
                    }
                }),
                Box.createHorizontalStrut(3),
                moodComboBox = new JComboBox(moodDropDownModel),
                Box.createHorizontalGlue()
            ),
            Box.createVerticalStrut(5),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                musicCheckbox = new JCheckBox(new AbstractAction(L10N.CHATTR_DIALOG_MUSIC_CHECKBOX.s()) {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        musicTextEdit.setEnabled(musicCheckbox.isSelected());
                    }
                }),
                Box.createHorizontalStrut(3),
                musicTextEdit,
                Box.createHorizontalGlue()
            ),
            Box.createVerticalStrut(5),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                locationCheckbox = new JCheckBox(new AbstractAction(L10N.CHATTR_DIALOG_LOCATION_CHECKBOX.s()) {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        locationTextEdit.setEnabled(locationCheckbox.isSelected());
                    }
                }),
                Box.createHorizontalStrut(3),
                locationTextEdit,
                Box.createHorizontalGlue()
            ),
            Box.createVerticalStrut(5),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                userpicCheckbox = new JCheckBox(new AbstractAction(L10N.CHATTR_DIALOG_USERPIC_CHECKBOX.s()) {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        userpicComboBox.setEnabled(userpicCheckbox.isSelected());
                    }
                }),
                Box.createHorizontalStrut(3),
                userpicComboBox = new JComboBox(userpicDropDownModel),
                Box.createHorizontalGlue()
            )
        );

        moodComboBox.setEditable(true);
        accessLevelComboBox.setRenderer(new AccessLevelListCellRenderer());

        accessLevelComboBox.setAction(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (m_internalAccessChange) {
                    return;
                }

                int index = accessLevelComboBox.getSelectedIndex();
                m_accessLevel = LJEventAccessLevel.getValue(index);

                if (index == 3) {
                    onEditAccessLevel();
                }
            }
        });

        SwingFactory.setEnabled(false, accessLevelComboBox, accessLevelButton, screenComboBox, moodComboBox,
                musicTextEdit, locationTextEdit, userpicComboBox);
        SwingFactory.setCommonPreferredSize(accessLevelCheckbox, screenCheckbox, moodCheckbox,
                musicCheckbox, locationCheckbox, userpicCheckbox);
        SwingFactory.setWidthMaximumToPreferred(accessLevelComboBox, userpicComboBox, screenComboBox);
        SwingFactory.setAlignmentX(panel, LEFT_ALIGNMENT);

        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(new OkCancelButtonPanel(this), BorderLayout.SOUTH);
    }

    public boolean changeAnything() {
        return changeAccessLevel() ||
               changeLocation() ||
               changeMusic() ||
               changeScreen() ||
               changeUserpic() ||
               changeMood();
    }

    public boolean changeAccessLevel() { return accessLevelCheckbox.isSelected(); }
    public LJEventAccessLevel getAccessLevel() { return m_accessLevel; }
    public int getGroupMask() { return m_groupMask; }

    public boolean changeLocation() { return locationCheckbox.isSelected(); }
    public String getLocationString() { return locationTextEdit.getText(); }

    public boolean changeMusic() { return musicCheckbox.isSelected(); }
    public String getMusic() { return musicTextEdit.getText(); }

    public boolean changeScreen() { return screenCheckbox.isSelected(); }
    public int getScreeningType() { return screenComboBox.getSelectedIndex(); }

    public boolean changeMood() { return moodCheckbox.isSelected(); }
    public int getMoodIndex() { return moodComboBox.getSelectedIndex(); }
    public String getMoodString() { return (String)moodComboBox.getSelectedItem(); }

    public boolean changeUserpic() { return userpicCheckbox.isSelected(); }
    public int getUserpicIndex() { return userpicComboBox.getSelectedIndex(); }

    private void onEditAccessLevel() {
        EventAccessLevelDialog dial = new EventAccessLevelDialog(getMainFrame(), m_accessLevel, m_groupMask, m_account);
        if (dial.showDialog()) {
            m_accessLevel = dial.getAccessLevel();
            m_groupMask = dial.getGroupMask();
            setupAccessLevelComboBox();
        }
    }

    private void setupAccessLevelComboBox() {
        m_internalAccessChange = true;
        accessLevelComboBox.setSelectedIndex(m_accessLevel.ordinal());
        m_internalAccessChange = false;
    }
}
